name: Tests

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    name: JS
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        browser: [chrome-headless, firefox-headless, webkit-headless]
        exclude:
          # Playwright webkit binary is not compatible with ubuntu
          - os: ubuntu-latest
            browser: webkit-headless
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1

      - name: Install dependencies
        shell: bash
        run: |
          set -eux
          npm install -g yarn
          yarn

      - name: Build Source
        shell: bash
        run: |
            set -eux
            yarn build
            yarn build:test

      - name: Run Tests
        run: |
          yarn run test:${{ matrix.browser }}

  build:
    runs-on: ubuntu-latest

    env:
      PLAYWRIGHT_BROWSERS_PATH: ${{ github.workspace }}/pw-browsers

    steps:
      - uses: actions/checkout@v4
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1

      - name: Set up browser cache
        uses: actions/cache@v4
        with:
          path: |
            ${{ github.workspace }}/pw-browsers
          key: ubuntu-latest-${{ hashFiles('./yarn.lock') }}

      - name: Install dependencies
        shell: bash
        run: |
          set -eux
          npm install -g yarn
          yarn

      - name: Check linters
        shell: bash
        run: |
            set -eux
            yarn lint:check

      - name: Build Source
        shell: bash
        run: |
            set -eux
            yarn build
            yarn build:test

      - name: Test Examples
        shell: bash
        run: |
          set -eux
          yarn minimize
          yarn build:examples
          # Install playwright for example tests
          npx playwright install chromium
          yarn test:examples

      - name: Build Docs
        run: |
          set -eux
          yarn clean
          yarn build:src
          yarn docs

      - name: Publish with Verdaccio
        run: |
          set -eux
          npm install -g verdaccio
          verdaccio --config .github/scripts/verdaccio.yml &
          npm set registry http://localhost:4873/
          yarn config set npmRegistryServer http://localhost:4873/
          yarn config set unsafeHttpWhitelist --json '["localhost"]'
          git config --global user.email "you@example.com"
          git config --global user.name "Your Name"
          yarn run update:versions patch --yes
          git commit -a -m "Update versions"
          yarn run publish
          yarn config unset npmRegistryServer
          yarn config unset unsafeHttpWhitelist

  check_links:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1
      - uses: jupyterlab/maintainer-tools/.github/actions/check-links@v1
        with:
          ignore_links: "./api/index.html examples/accordionpanel/index.html examples/datagrid/index.html examples/dockpanel/index.html examples/menubar/index.html https://mybinder.org/v2/gh/jupyterlab/lumino/.*"

  check_release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Bump artificially the top version
        shell: python
        run: |
          import json
          from datetime import datetime

          with open("package.json") as f:
            data = json.load(f)

          data["version"] = datetime.now().strftime("%Y.%m.%d")
          data["jupyter-releaser"] = { "options": { "version-cmd": "npm run update:versions -- --yes" } }

          with open("package.json", "w") as f:
            data = json.dump(data, f)

      - name: Check Release
        uses: jupyter-server/jupyter_releaser/.github/actions/check-release@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
